package com.booksystem.controller;

import com.booksystem.common.RestResult;
import com.booksystem.dto.CreateOrderDTO;
import com.booksystem.dto.QueryOrderDetailDTO;
import com.booksystem.dto.UpdateOrderStatusDTO;
import com.booksystem.entity.OrderInfoDO;
import com.booksystem.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *   订单控制器
 * </p>
 * @author 段伟豪
 */
@Slf4j
@Api(tags = "订单管理")
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     *
     * @param createOrderDTO 创建订单参数
     * @return 统一响应结果
     */
    @PostMapping("/create")
    @ApiOperation("创建订单")
    public RestResult<Boolean> createOrder(@RequestBody @Validated CreateOrderDTO createOrderDTO) {
        log.info("开始创建订单，入参={}", createOrderDTO);
        RestResult<Boolean> result = orderService.createOrder(createOrderDTO);
        log.info("创建订单结束，返回结果={}", result);
        return result;
    }

    /**
     * 查询订单详情
     *
     * @param queryOrderDetailDTO 查询订单详情参数
     * @return 统一响应结果
     */
    @GetMapping("/detail")
    @ApiOperation("查询订单详情")
    public RestResult<OrderInfoDO> queryOrderDetail(@Validated QueryOrderDetailDTO queryOrderDetailDTO) {
        log.info("开始查询订单详情，入参={}", queryOrderDetailDTO);
        RestResult<OrderInfoDO> result = orderService.queryOrderDetail(queryOrderDetailDTO);
        log.info("查询订单详情结束，返回结果={}", result);
        return result;
    }

    /**
     * 更新订单状态
     *
     * @param updateOrderStatusDTO 更新订单状态参数
     * @return 统一响应结果
     */
    @PutMapping("/status")
    @ApiOperation("更新订单状态")
    public RestResult<Boolean> updateOrderStatus(@RequestBody @Validated UpdateOrderStatusDTO updateOrderStatusDTO) {
        log.info("开始更新订单状态，入参={}", updateOrderStatusDTO);
        RestResult<Boolean> result = orderService.updateOrderStatus(updateOrderStatusDTO);
        log.info("更新订单状态结束，返回结果={}", result);
        return result;
    }

    /**
     * 取消订单
     *
     * @param orderId 订单ID
     * @return 统一响应结果
     */
    @DeleteMapping("/{orderId}")
    @ApiOperation("取消订单")
    public RestResult<Boolean> cancelOrder(@PathVariable @NotNull(message = "订单ID不能为空") Long orderId) {
        log.info("开始取消订单，入参={}", orderId);
        RestResult<Boolean> result = orderService.cancelOrder(orderId);
        log.info("取消订单结束，返回结果={}", result);
        return result;
    }
}